<?php
namespace App\Repos;
use App\Models\Cate;
use App\Models\Product;
use App\Models\ProductImg;
use App\Models\ProductTag;
use App\Models\ProductSpec;
use App\Models\Order;
use App\Models\OrderProduct;
use App\Models\ProductGroup;
use App\Models\ProductCity;
use App\Models\System;
use App\Models\Tag;
use App\Models\AdminHandlerLog;
use App\Models\OrderGroup;
use App\Models\City;
use Carbon\Carbon;
use DB,Log,OSS;
use App\Events\AdminHanderEvent;


class ProductRepo 
{    
    public static function create($data,$pics=[])
    {
        $rs['status'] = false;
        
       
        try {
            
            //dd($data);
            DB::transaction(function () use ($data,$pics) {
               
                $data['code'] = str_random(8);
                $product = Product::create($data);               
                
                if($pics) {
                    foreach ($pics as $p) {
                        $pic['product_id'] = $product->id;
                        $pic['thumb']  = 'thumb_'.$p;
                        $pic['imgurl'] = $p;
                        ProductImg::create($pic);
                    }
                }
            });
            $rs['status'] = true;    
            return $rs;
            
        } catch (\Exception $e) {
            dd($e->getMessage());
            $rs['msg'] = $e->getMessage();
            return $rs;
        }
        $rs['status'] = true;
        return $rs;
    } 

    public static function update($id,$data)
    {
        $rs['status'] = false;
        try {
            DB::transaction(function () use ($id,$data) {
                $product = Product::where(['id'=>$id])->update($data);
            });
            $rs['status'] = true;    
            return $rs;
            
        } catch (\Exception $e) {

            $rs['msg'] = $e->getMessage();
            return $rs;
        }
        $rs['status'] = true;
        return $rs;
    }   
  

    public static function getListPages($where=[],$num=10) 
    {
    	$query = Product::query();        
        if(array_key_exists('name', $where) ) {           
            $query->where('name','like','%'.$where['name'].'%');
            unset($where['name']);
        }
        if(array_get($where,'status')==='') {
            $where['status'] = 1;    
        }
        $query->with('imgs:id,product_id,imgurl');       
        $query = $query->where($where);
       
        
        
        $list  = $query->orderBy('created_at','desc')
        					->paginate($num);
        return $list;					
    }

    public static function pages($where=[],$num=10) 
    {
        $query = Product::query();        
        

        $where['status'] = 1; 
         
        $query = $query->where($where);
        
        
        
        $list  = $query->orderBy('created_at','desc')
                            ->paginate($num);
        return $list;                   
    }

    public static  function destroy($id)
    {
        $rs['status'] = false;
        Product::destroy($id);
        
        $imgs = ProductImg::where(['product_id'=>$id])->get();
        foreach ($imgs as $k => $img) { 
            if($img->thumb) {
                OSS::deleteObject("xmistz", $img->thumb);   
            }  

            if($img->imgurl) {
                OSS::deleteObject("xmistz", $img->imgurl);  
            } 
             
            $img->delete();
        }
        
       
        $rs['status'] = true;
       
        return $rs;
    }

    public static function setThumb($id,$pic)
    {
        $product = Product::find($id);
        $product->thumb = $pic;
        $product->save();
        return true;
    }

     

    public static function find($where)
    {
        $data = Product::where($where)
                        ->first();
        return $data;
    }   

    public static function imgs($product_id)
    {
        $where['product_id'] = $product_id;
        $fields = ['id','product_id','imgurl'];
        return ProductImg::where($where)->select($fields)->get();
    }


    // 修改产品上下架状态
    public static function ChangeStatus($id,$data)
    {        
        Product::where(['id'=>$id])->update($data);
        return true;
    }

  

    
    // 根据 tag查询商品列表
    public static function getListByTag($where,$order=[])
    {
        $product_ids = DB::table('product_tags as pt')           
            ->join('tags as t','t.id','=','pt.tag_id')
            ->where('t.name','=',$where['keyword'])
            ->pluck('pt.product_id')
            ->toArray();
        //dd($product_ids,$order);    
        $query = Product::query();
        $query->where(function($query) use($product_ids,$where){
            $query->whereIn('id',$product_ids)
            ->orWhere('name','like','%'.$where['keyword'].'%');
        });
        $city = array_pull($where,'city');
        

        if($order) {
            $query->orderBy($order['orderby'],$order['orderway']);
        }
        $list = $query->where('status',1)
                        ->paginate(10);
        return $list;    
    } 


   
    

    

    



  
}
